package com.tencent.cloud.uikit.ui.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.iov.util.ViewStateUtils;
import com.tencent.cloud.uikit.R;

/**
 * 自定义标题栏布局
 *
 * @author showxiao
 * @date 2018/8/15
 */
public class IovToolbar extends RelativeLayout {

    private View mBackContainer;
    private ImageView mBackImageView;
    private TextView mBackTextView;

    private TextView mTitleView;

    private View mMenuContainer;
    private ImageView mMenuImageView;
    private TextView mMenuTextView;

    private RelativeLayout mCustomViewContainer;
    private View mCustomView;

    private boolean mIsCloseOnRight;
    private String mMenuText;

    public IovToolbar(Context context) {
        super(context);
        init(context);
    }

    public IovToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public IovToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.iov_tool_bar_content, this, true);

        mBackContainer = findViewById(R.id.ll_back_container);
        mBackImageView = mBackContainer.findViewById(R.id.iv_back_icon);
        mBackTextView = mBackContainer.findViewById(R.id.tv_back_text);

        mTitleView = findViewById(R.id.tv_title);

        mMenuContainer = findViewById(R.id.ll_menu_container);
        mMenuImageView = mMenuContainer.findViewById(R.id.iv_menu_icon);
        mMenuTextView = mMenuContainer.findViewById(R.id.tv_menu_text);

        mCustomViewContainer = findViewById(R.id.rl_custom_view_container);

        ViewStateUtils.setUpViewPressState(mBackContainer, mMenuContainer);

        mIsCloseOnRight = false;
    }

    public void setTitle(@StringRes int resId) {
        setTitle(ResourcesUtils.getString(resId));
    }

    public void setTitle(CharSequence title) {
        if (mCustomView != null) {
            mCustomViewContainer.setVisibility(GONE);
        }

        mTitleView.setText(title);
    }

    public void setHomeAsUp(@StringRes int resId, OnClickListener listener) {
        setHomeAsUp(ResourcesUtils.getString(resId), listener);
    }

    public void setHomeAsUp(CharSequence text, OnClickListener listener) {
        setHomeAsUpText(text);
        setHomeAsUpListener(listener);
    }

    public void setHomeAsUpIndicator(@DrawableRes int resId) {
        setHomeAsUpIndicator(ResourcesUtils.getDrawable(resId));
    }

    public void setHomeAsUpIndicator(@Nullable Drawable indicator) {
        mBackImageView.setImageDrawable(indicator);
    }

    public void setHomeAsUpText(@StringRes int resId) {
        setHomeAsUpText(ResourcesUtils.getString(resId));
    }

    public void setHomeAsUpText(CharSequence text) {
        mBackTextView.setText(text);
    }

    public void setHomeAsUpListener(OnClickListener listener) {
        mBackContainer.setOnClickListener(listener);
    }

    public void showHomeAsUpIcon() {
        mBackImageView.setVisibility(VISIBLE);
    }

    public void hideHomeAsUpIcon() {
        mBackImageView.setVisibility(GONE);
    }

    public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) {
        mBackContainer.setVisibility(showHomeAsUp ? VISIBLE : INVISIBLE);
    }


    public void setMenu(@StringRes int resId, OnClickListener listener) {
        setMenu(ResourcesUtils.getString(resId), listener);
    }

    public void setMenu(CharSequence text, OnClickListener listener) {
        setMenuText(text);
        setMenuClickListener(listener);
    }

    public void setMenuIcon(@DrawableRes int resId) {
        setMenuIcon(ResourcesUtils.getDrawable(resId));
    }

    public void setMenuIcon(Drawable drawable) {
        mMenuImageView.setImageDrawable(drawable);
    }

    public void setMenuText(@StringRes int resId) {
        setMenuText(ResourcesUtils.getString(resId));
    }

    public void setMenuText(CharSequence text) {
        mMenuTextView.setText(text);
    }

    public void setMenuClickListener(OnClickListener listener) {
        mMenuContainer.setOnClickListener(listener);
    }

    public void setMenuEnabled(boolean enabled) {
        ViewStateUtils.setUpViewEnableState(enabled, mMenuContainer);
        mMenuContainer.setEnabled(enabled);
    }

    public void setCloseOnRight(boolean closeOnRight) {
        if (mIsCloseOnRight == closeOnRight) {
            return;
        }

        mIsCloseOnRight = closeOnRight;

        mBackContainer.setVisibility(INVISIBLE);

        setMenuIcon(R.drawable.con_icon_close);
        setMenuClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mBackContainer.callOnClick();
            }
        });
    }

    public void setCustomView(View view) {
        if (view == null) {
            mTitleView.setVisibility(VISIBLE);
            mMenuContainer.setVisibility(VISIBLE);
            mCustomViewContainer.setVisibility(GONE);
            mCustomView = null;
            return;
        }

        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams == null) {
            layoutParams = new LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
            );
        }
        LayoutParams lp = new LayoutParams(layoutParams);

        mTitleView.setVisibility(INVISIBLE);
        mMenuContainer.setVisibility(INVISIBLE);
        mCustomViewContainer.setVisibility(VISIBLE);

        mCustomViewContainer.removeAllViews();
        mCustomViewContainer.addView(view, lp);
        mCustomView = view;
    }

    public TextView getmMenuTextView() {
        return mMenuTextView;
    }

    public ImageView getmBackImageView() {
        return mBackImageView;
    }

    public TextView getmBackTextView() {
        return mBackTextView;
    }
}
